package com.senseuni.da.ui

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.senseuni.da.MainActivity
import com.senseuni.da.R
import com.senseuni.da.adapter.SettingMenuAdapter
import com.senseuni.da.databinding.ActivitySettingBinding
import com.senseuni.da.model.SettingMenuItem


class SettingFragment : Fragment() {

    companion object {
        fun newInstance() = SettingFragment()
    }

    private lateinit var binding: ActivitySettingBinding

    private var mActivity: MainActivity? = null

    private lateinit var mFragments: MutableList<Fragment>

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as MainActivity?
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = ActivitySettingBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        binding.ivBack.setOnClickListener { mActivity?.settingClose() }

        val adapter = SettingMenuAdapter(requireContext())
        binding.recyclerView.adapter = adapter
        val layoutManager = LinearLayoutManager(requireContext())
        binding.recyclerView.layoutManager = layoutManager

        val menuList = mutableListOf<SettingMenuItem>()
        menuList.add(SettingMenuItem(R.drawable.ic_my_photo_frame, getString(R.string.my_album)))
//        menuList.add(SettingMenuItem(R.drawable.ic_manager_photo_frame, getString(R.string.manager_album)))
//        menuList.add(SettingMenuItem(R.drawable.ic_my_friend, getString(R.string.my_friend)))
        menuList.add(SettingMenuItem(R.drawable.ic_display, getString(R.string.display)))
        menuList.add(SettingMenuItem(R.drawable.ic_ppt, getString(R.string.ppt)))
        menuList.add(SettingMenuItem(R.drawable.ic_wifi, getString(R.string.wifi)))
        menuList.add(SettingMenuItem(R.drawable.ic_notify, getString(R.string.notification)))
        menuList.add(SettingMenuItem(R.drawable.ic_backup_restore, getString(R.string.backup_store)))
//        menuList.add(SettingMenuItem(R.drawable.ic_help, getString(R.string.help)))
        menuList.add(SettingMenuItem(R.drawable.ic_about, getString(R.string.about)))
        adapter.setDataList(menuList)

        mFragments = arrayListOf()
        mFragments.add(MyAlbumFragment.newInstance())
//        mFragments.add(ImageManagerFragment.newInstance())
//        mFragments.add(BlankFragment.newInstance())
//        mFragments.add(BlankFragment.newInstance())
        mFragments.add(DisplayFragment.newInstance())
        mFragments.add(SlideFragment.newInstance())
        mFragments.add(WiFiSettingFragment.newInstance())
        mFragments.add(NotificationFragment.newInstance())
        mFragments.add(BackupRestoreFragment.newInstance())
//        mFragments.add(BlankFragment.newInstance())
        mFragments.add(AboutFragment.newInstance())

        adapter.setOnItemClickListener { item, position ->
            replaceFragment(mFragments[position])
        }

        replaceFragment(mFragments[0])
    }

    //Fragment替换
    private fun replaceFragment(fragment: Fragment) {
        val fragmentManager = activity?.supportFragmentManager ?: return
        val fragmentTransaction = fragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.content_container, fragment)
//        fragmentTransaction.addToBackStack(null)
        fragmentTransaction.commit()
    }

    fun switchToFragment(fragment: ImageUSBTransferFragment) {
        val fragmentManager = activity?.supportFragmentManager ?: return
        val fragmentTransaction = fragmentManager.beginTransaction()
        fragmentTransaction.add(R.id.content_container, fragment)
        fragmentTransaction.addToBackStack(null)
        fragmentTransaction.commit()
    }

}